﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Drawing;
using fideeraSoft.Utils;
using System.Windows.Forms;
using fideeraSoft.Entities;
using fideeraSoft.Forms;

namespace fideeraSoft.Paneles
{
    class RemitoPanelAlta:PanelGeneral
    {
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btSubmit;
        private System.Windows.Forms.GroupBox gbGral;
        private System.Windows.Forms.Label lbId;
        private System.Windows.Forms.Label lbIdRemito;
        private System.Windows.Forms.Label lbUsuario;
        private System.Windows.Forms.DateTimePicker dtFechaRemito;
        private System.Windows.Forms.Label lbFechaRemito;
        private System.Windows.Forms.Label lbCodigo;
        private System.Windows.Forms.TextBox tbCodigo;
        private System.Windows.Forms.Label lbProveedorNombre;
        private System.Windows.Forms.Button btVolver;
        
        private TableLayoutPanel tlpGral;
        
        private Button btProveedor;
        private Button btAgregarLote;
        private ListBox lsbLote;
        private TableLayoutPanel tlpListaLote;
        private Button btRemoverLote;
        int posicionTablaBotones;

        private Proveedor prov;
        private DateTimePicker dtFechaRecepcion;
        private Label lbFechaRecepcion;
        private Label lbUserName;

        private List<Lote> lstLotes;

        public RemitoPanelAlta(Handler handler)
        {

             lstLotes = new List<Lote>();
            

            this.formatearPanelGeneral(handler);
            InitializeComponent();
            this.formatearTableTitulo("ALTA DE REMITO DE ENTRADA");

            this.formatearTablaListaLote();
            this.formatearGroupBoxGral();
            this.formatearTablaGral();
            
            this.formatearTablaBotones();

            this.Controls.Add(gbGral);
            this.Controls.Add(tlpBotones);

        }

        #region FORMATEO 
        private void formatearGroupBoxGral()
        {
            

            gbGral.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, gbGral.Size.Width), this.InitialRealPointVertical);

            gbGral.Controls.Add(tlpGral);
        }


        private void formatearTablaGral()
        {

            tlpGral.ColumnStyles.Clear();
            for (int i = 0; i < tlpGral.ColumnCount; i++)
            {
                tlpGral.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            }

            tlpGral.RowStyles.Clear();
            for (int i = 0; i < tlpGral.RowCount; i++)
            {
                tlpGral.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }
            
            //tlpForm.Size = new Size((gbAltaProveedor.Size.Width - 10));
                        
            tlpGral.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpGral.Size.Width), 30);
           
           

            cargarControlesEnTabla();
            
        }

        private void cargarControlesEnTabla()
        {
            tlpGral.Controls.Add(lbId);

            lbIdRemito.Text = this.handler.buscarNuevoUltimoIdRemitoEntradaEnBD().ToString();
            tlpGral.Controls.Add(lbIdRemito);

            

            tlpGral.Controls.Add(lbCodigo);           
            tlpGral.Controls.Add(tbCodigo);
                      

            tlpGral.Controls.Add(lbFechaRemito);
            dtFechaRemito.Value = System.DateTime.Now;
            tlpGral.Controls.Add(dtFechaRemito);

            tlpGral.Controls.Add(lbFechaRecepcion);
            dtFechaRecepcion.Value = System.DateTime.Now;
            tlpGral.Controls.Add(dtFechaRecepcion);

            tlpGral.Controls.Add(lbUsuario);
            lbUserName.Text = UsuarioLoggin.username;
            tlpGral.Controls.Add(lbUserName);

            tlpGral.Controls.Add(btProveedor);
            tlpGral.Controls.Add(lbProveedorNombre);

            tlpGral.Controls.Add(btAgregarLote);
            //tlpGral.Controls.Add(lsbLote);
            tlpGral.Controls.Add(tlpListaLote);

        }
        private void formatearTablaListaLote()
        {
            tlpListaLote.Controls.Add(lsbLote);
            tlpListaLote.Controls.Add(btRemoverLote);
        }
        private void formatearTablaBotones()
        {
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btSubmit);

            posicionTablaBotones = InitialRealPointVertical + gbGral.Size.Height + 40;
            tlpBotones.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBotones.BackColor = Color.LightCyan;
            //tlpBotones.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBotones.Size.Width), posicionTablaBotones);

        }
        #endregion

        private void InitializeComponent()
        {
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btSubmit = new System.Windows.Forms.Button();
            this.btVolver = new System.Windows.Forms.Button();
            this.gbGral = new System.Windows.Forms.GroupBox();
            this.lbId = new System.Windows.Forms.Label();
            this.lbIdRemito = new System.Windows.Forms.Label();
            this.lbUsuario = new System.Windows.Forms.Label();
            this.dtFechaRemito = new System.Windows.Forms.DateTimePicker();
            this.lbFechaRemito = new System.Windows.Forms.Label();
            this.lbCodigo = new System.Windows.Forms.Label();
            this.tbCodigo = new System.Windows.Forms.TextBox();
            this.lbProveedorNombre = new System.Windows.Forms.Label();
            this.tlpGral = new System.Windows.Forms.TableLayoutPanel();
            this.btProveedor = new System.Windows.Forms.Button();
            this.btAgregarLote = new System.Windows.Forms.Button();
            this.lsbLote = new System.Windows.Forms.ListBox();
            this.tlpListaLote = new System.Windows.Forms.TableLayoutPanel();
            this.btRemoverLote = new System.Windows.Forms.Button();
            this.dtFechaRecepcion = new System.Windows.Forms.DateTimePicker();
            this.lbFechaRecepcion = new System.Windows.Forms.Label();
            this.lbUserName = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSize = true;
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 100);
            this.tlpBotones.TabIndex = 0;
            // 
            // btSubmit
            // 
            this.btSubmit.BackColor = System.Drawing.Color.AliceBlue;
            this.btSubmit.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btSubmit.ForeColor = System.Drawing.Color.Black;
            this.btSubmit.Location = new System.Drawing.Point(0, 0);
            this.btSubmit.Name = "btSubmit";
            this.btSubmit.Size = new System.Drawing.Size(75, 40);
            this.btSubmit.TabIndex = 0;
            this.btSubmit.Text = "Guardar";
            this.btSubmit.UseVisualStyleBackColor = false;
            this.btSubmit.Click += new System.EventHandler(this.btSubmit_Click);
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // gbGral
            // 
            this.gbGral.Font = new System.Drawing.Font("Bodoni MT", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbGral.Location = new System.Drawing.Point(0, 0);
            this.gbGral.Name = "gbGral";
            this.gbGral.Size = new System.Drawing.Size(800, 550);
            this.gbGral.TabIndex = 0;
            this.gbGral.TabStop = false;
            this.gbGral.Text = "Remito Entrada";
            // 
            // lbId
            // 
            this.lbId.AutoSize = true;
            this.lbId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbId.Location = new System.Drawing.Point(0, 0);
            this.lbId.Name = "lbId";
            this.lbId.Size = new System.Drawing.Size(100, 23);
            this.lbId.TabIndex = 0;
            this.lbId.Text = "ID de Remito de Entrada";
            this.lbId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbIdRemito
            // 
            this.lbIdRemito.AutoSize = true;
            this.lbIdRemito.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbIdRemito.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbIdRemito.Location = new System.Drawing.Point(0, 0);
            this.lbIdRemito.Name = "lbIdRemito";
            this.lbIdRemito.Size = new System.Drawing.Size(100, 23);
            this.lbIdRemito.TabIndex = 0;
            this.lbIdRemito.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lbUsuario
            // 
            this.lbUsuario.AutoSize = true;
            this.lbUsuario.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbUsuario.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbUsuario.Location = new System.Drawing.Point(0, 0);
            this.lbUsuario.Name = "lbUsuario";
            this.lbUsuario.Size = new System.Drawing.Size(100, 23);
            this.lbUsuario.TabIndex = 0;
            this.lbUsuario.Text = "Usuario";
            this.lbUsuario.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // dtFechaRemito
            // 
            this.dtFechaRemito.Location = new System.Drawing.Point(0, 0);
            this.dtFechaRemito.Name = "dtFechaRemito";
            this.dtFechaRemito.Size = new System.Drawing.Size(350, 20);
            this.dtFechaRemito.TabIndex = 0;
            // 
            // lbFechaRemito
            // 
            this.lbFechaRemito.AutoSize = true;
            this.lbFechaRemito.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbFechaRemito.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbFechaRemito.Location = new System.Drawing.Point(0, 0);
            this.lbFechaRemito.Name = "lbFechaRemito";
            this.lbFechaRemito.Size = new System.Drawing.Size(100, 23);
            this.lbFechaRemito.TabIndex = 0;
            this.lbFechaRemito.Text = "Fecha del Remito";
            this.lbFechaRemito.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbCodigo
            // 
            this.lbCodigo.AutoSize = true;
            this.lbCodigo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCodigo.Location = new System.Drawing.Point(0, 0);
            this.lbCodigo.Name = "lbCodigo";
            this.lbCodigo.Size = new System.Drawing.Size(100, 23);
            this.lbCodigo.TabIndex = 0;
            this.lbCodigo.Text = "Código Remito";
            this.lbCodigo.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbCodigo
            // 
            this.tbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCodigo.Location = new System.Drawing.Point(0, 0);
            this.tbCodigo.MaxLength = 10;
            this.tbCodigo.Name = "tbCodigo";
            this.tbCodigo.Size = new System.Drawing.Size(200, 30);
            this.tbCodigo.TabIndex = 0;
            // 
            // lbProveedorNombre
            // 
            this.lbProveedorNombre.AutoSize = true;
            this.lbProveedorNombre.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbProveedorNombre.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbProveedorNombre.Location = new System.Drawing.Point(0, 0);
            this.lbProveedorNombre.Name = "lbProveedorNombre";
            this.lbProveedorNombre.Size = new System.Drawing.Size(100, 23);
            this.lbProveedorNombre.TabIndex = 0;
            this.lbProveedorNombre.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // tlpGral
            // 
            this.tlpGral.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpGral.BackColor = System.Drawing.Color.LightCyan;
            this.tlpGral.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpGral.ColumnCount = 2;
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.Location = new System.Drawing.Point(0, 0);
            this.tlpGral.Name = "tlpGral";
            this.tlpGral.RowCount = 8;
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.Size = new System.Drawing.Size(700, 480);
            this.tlpGral.TabIndex = 0;
            // 
            // btProveedor
            // 
            this.btProveedor.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btProveedor.BackColor = System.Drawing.Color.AliceBlue;
            this.btProveedor.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btProveedor.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btProveedor.Location = new System.Drawing.Point(0, 0);
            this.btProveedor.Name = "btProveedor";
            this.btProveedor.Size = new System.Drawing.Size(75, 23);
            this.btProveedor.TabIndex = 0;
            this.btProveedor.Text = "Proveedor";
            this.btProveedor.UseVisualStyleBackColor = false;
            this.btProveedor.Click += new System.EventHandler(this.btProveedor_Click);
            // 
            // btAgregarLote
            // 
            this.btAgregarLote.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btAgregarLote.BackColor = System.Drawing.Color.AliceBlue;
            this.btAgregarLote.Dock = System.Windows.Forms.DockStyle.Top;
            this.btAgregarLote.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btAgregarLote.Location = new System.Drawing.Point(0, 0);
            this.btAgregarLote.Name = "btAgregarLote";
            this.btAgregarLote.Size = new System.Drawing.Size(75, 23);
            this.btAgregarLote.TabIndex = 0;
            this.btAgregarLote.Text = "Agregar Lote";
            this.btAgregarLote.UseVisualStyleBackColor = false;
            this.btAgregarLote.Click += new System.EventHandler(this.btAgregarLote_Click);
            // 
            // lsbLote
            // 
            this.lsbLote.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lsbLote.FormattingEnabled = true;
            this.lsbLote.Location = new System.Drawing.Point(0, 0);
            this.lsbLote.Name = "lsbLote";
            this.lsbLote.Size = new System.Drawing.Size(220, 250);
            this.lsbLote.TabIndex = 0;
            // 
            // tlpListaLote
            // 
            this.tlpListaLote.ColumnCount = 2;
            this.tlpListaLote.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 60F));
            this.tlpListaLote.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 40F));
            this.tlpListaLote.Location = new System.Drawing.Point(0, 0);
            this.tlpListaLote.Name = "tlpListaLote";
            this.tlpListaLote.RowCount = 1;
            this.tlpListaLote.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpListaLote.Size = new System.Drawing.Size(400, 260);
            this.tlpListaLote.TabIndex = 0;
            // 
            // btRemoverLote
            // 
            this.btRemoverLote.BackColor = System.Drawing.Color.AliceBlue;
            this.btRemoverLote.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btRemoverLote.Location = new System.Drawing.Point(0, 0);
            this.btRemoverLote.Name = "btRemoverLote";
            this.btRemoverLote.Size = new System.Drawing.Size(130, 23);
            this.btRemoverLote.TabIndex = 0;
            this.btRemoverLote.Text = "Remover Lote";
            this.btRemoverLote.UseVisualStyleBackColor = false;
            this.btRemoverLote.Click += new System.EventHandler(this.btRemoverLote_Click);
            // 
            // dtFechaRecepcion
            // 
            this.dtFechaRecepcion.Location = new System.Drawing.Point(0, 0);
            this.dtFechaRecepcion.Name = "dtFechaRecepcion";
            this.dtFechaRecepcion.Size = new System.Drawing.Size(350, 20);
            this.dtFechaRecepcion.TabIndex = 0;
            // 
            // lbFechaRecepcion
            // 
            this.lbFechaRecepcion.AutoSize = true;
            this.lbFechaRecepcion.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbFechaRecepcion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbFechaRecepcion.Location = new System.Drawing.Point(0, 0);
            this.lbFechaRecepcion.Name = "lbFechaRecepcion";
            this.lbFechaRecepcion.Size = new System.Drawing.Size(100, 23);
            this.lbFechaRecepcion.TabIndex = 0;
            this.lbFechaRecepcion.Text = "Fecha de Recepción";
            this.lbFechaRecepcion.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbUserName
            // 
            this.lbUserName.AutoSize = true;
            this.lbUserName.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbUserName.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbUserName.Location = new System.Drawing.Point(0, 0);
            this.lbUserName.Name = "lbUserName";
            this.lbUserName.Size = new System.Drawing.Size(100, 23);
            this.lbUserName.TabIndex = 0;
            this.lbUserName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.ResumeLayout(false);

        }

        private void btProveedor_Click(object sender, EventArgs e)
        {
            prov = new Proveedor();
            new FormAddProveedorARemito(prov,lbProveedorNombre);
            
             

        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btSubmit_Click(object sender, EventArgs e)
        {

            if ( validarCamposRemito())
            {
                if((lsbLote!=null)&&(lsbLote.Items.Count>0)&&(lstLotes!=null)&&(lstLotes.Count>0))
                {
                    RemitoEntrada remitoEntrada = new RemitoEntrada();

                    remitoEntrada.Codigo = tbCodigo.Text;
                    remitoEntrada.IdRemitoEntrada = Convert.ToInt32(lbIdRemito.Text);
                    remitoEntrada.DtFechaRemito = dtFechaRemito.Value.Date;
                    remitoEntrada.DtFechaRecepcion = dtFechaRecepcion.Value.Date;

                    remitoEntrada.Usuario = MetodosGenerales.getUsuarioActual();
                    remitoEntrada.Proveedor = prov;

                    
                    handler.altaRemitoEntrada(remitoEntrada);

                    foreach(Lote lote in lstLotes)
                    {
                        lote.FechaRecepcion = remitoEntrada.DtFechaRecepcion;

                        remitoEntrada.Lotes.Add(lote);
                        
                        handler.altaLote(lote);

                        foreach(MateriaPrima materiaPrima in lote.LstMateriaPrima)
                        {
                            handler.altaMateriaPrima_X_Lote(materiaPrima, lote, handler.buscarNuevoUltimoIdMateriaPrimaXLoteEnBD());
                            
                        }
                        
                        handler.altaRemitoEntrada_X_Lote(remitoEntrada, lote, handler.buscarNuevoUltimoIdRemitoEntradaXLoteEnBD()); //Para que le paso el lote si ya lo tiene el remito de entrada...
                    }                    

                    

                    limpiarTextBoxs();

                    MessageBox.Show("Remito cargado correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);    


                }else
                {
                    MessageBox.Show("No hay lotes cargados en el remito", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    
                }
            }
            else             
            {
                MessageBox.Show("Campos de remito no ingresados o incorrectos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    
            }

        }




        private void limpiarTextBoxs()
        {
            tbCodigo.Clear();
            lsbLote.Items.Clear();
            lstLotes.Clear();
            lbIdRemito.Text = handler.buscarNuevoUltimoIdRemitoEntradaEnBD().ToString();
            lbProveedorNombre.Text = "";

            lbUserName.Text = "";

            dtFechaRecepcion.Value = System.DateTime.Now;
            dtFechaRemito.Value = System.DateTime.Now;
            //UsuarioLoggin
           //bUserName.Text = MetodosGenerales.getUsuarioActual();
            lbUserName.Text = UsuarioLoggin.username;

        }

        private bool validarCamposRemito()
        {
            return !string.IsNullOrWhiteSpace(lbIdRemito.Text) && !string.IsNullOrWhiteSpace(lbUserName.Text) && !string.IsNullOrWhiteSpace(tbCodigo.Text) && (tbCodigo.Text.Length >= 5) && (dtFechaRemito.Value != null) && !string.IsNullOrWhiteSpace(prov.RazonSocial) && (prov != null) && !string.IsNullOrWhiteSpace(lbProveedorNombre.Text) && (dtFechaRecepcion!=null) ;
        }

        private void btAgregarLote_Click(object sender, EventArgs e)
        {
            //List<Lote> lstLotes=new List<Lote>();

            //lsbLote = new ListBox();

            new FormAddLoteARemito(lstLotes, lsbLote);
            
        }

        private void btRemoverLote_Click(object sender, EventArgs e)
        {

            if (lsbLote.Items.Count <= 0)
            {
                MessageBox.Show("NO HAY NINGÚN LOTE CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if((lsbLote.SelectedIndex != null)&&(lsbLote.SelectedItem!=null))
                {
                    string strItem = lsbLote.Items[lsbLote.SelectedIndex].ToString();

                    int start = strItem.IndexOf(": ") + 2;
                    int lenght = strItem.Length - start;

                  //  MessageBox.Show("INDEX: " + start + " - LENGH: " + lenght + " - ITEM: " + strItem.Substring(start, lenght), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    foreach (Lote lote in lstLotes)
                    {
                        if (lote.IdLote == Convert.ToInt32(strItem.Substring(start, lenght)))
                        {
                            lstLotes.Remove(lote);
                            break;
                        }

                    }

                    lsbLote.Items.Remove(lsbLote.SelectedItem);
                }
                else 
                {
                    MessageBox.Show("NO HAY NINGÚN LOTE SELECCIONADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }

        }
    }

}
